//定义两个借书正确与错误的方法类
class Book(val title:String,val author:String,val isbn:String){
    private var _borrowed:Boolean = false

    def borrowed:Boolean = _borrowed
    def borrowed():Unit = {
        _borrowed = true
    }
    def returnBook(): Unit ={
        _borrowed = false
    }
}
//定义面对对象book类
object  Book{
    def apply(title:String,author:String,isbn:String):Book = {
        new Book(title,author,isbn)
    }
    def unapply(book:Book):Option[(Strig,String,String)] = {
        Some(book.title,book.author,book.isbn)
    }
}
//查询书籍是否被借阅，是否图书馆有这本书。
class Library(private var books:List[Book] = Nil){
    def addBook(book:Book):Unit = {
        books = book::books
    }
    def borrowedBook(isbn:String):Unit = {
        val index = findBookIndexByIsbn(isbn)
        if(index>=0) {
            val book = books(index)
            if(!book.borrowed){
               book.borrow()
        println(s"The book with ISBN $isbn is borrowed.")
      } else {
        println("The book is not available.")
      }
    } else {
      println("The book is not found.")
    }
  }
//返回图书是否被还回，还是没有被借出。
  def returnBook(isbn: String): Unit = {
    val index = findBookIndexByIsbn(isbn)
    if (index >= 0) {
      val book = books(index)
      if (book.borrowed) {
        book.returnBook()
        println(s"The book with ISBN $isbn is returned.")
      } else {
        println("The book is not borrowed from this library.")
      }
    } else {
      println("The book is not found.")
    }
  }

  private def findBookIndexByIsbn(isbn: String): Int = {
    var index = 0
    while (index < books.length && books(index).isbn != isbn) {
      index += 1
    }
    if (index < books.length) index else -1
  }
}

object Library {
  def apply(books: Book*): Library = {
    new Library(books.toList)
  }
}

            
        
